package swing.draw;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;

import carlyang.Out;


public class SimpleDraw extends JFrame implements ActionListener,MouseListener{

	int x1,y1,x2,y2;
	int paintMode = 1;
	List<Shape> shapes = new ArrayList<>();
	String shapeType = "Rectangle";
	
	public SimpleDraw(int mode) {
		// TODO Auto-generated constructor stub
		if (mode == 1) {
			paintMode = 1;
			this.setTitle("Simple Draw,mode 1");
		}
		else if (mode == 2) {
			paintMode = 2;
			this.setTitle("Simple Draw,mode 2");
		}
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		ButtonGroup cbg = new ButtonGroup();
		JRadioButton lineButton = new JRadioButton("Line");
		JRadioButton ovalButton = new JRadioButton("Oval");
		JRadioButton rectangleButton = new JRadioButton("Rectangle");
		//ButtonGroup是逻辑上的，不是用来显示的，添加时应该不需要按顺序
		cbg.add(lineButton);
		cbg.add(ovalButton);
		cbg.add(rectangleButton);
		
		lineButton.addActionListener(this);
		ovalButton.addActionListener(this);
		rectangleButton.addActionListener(this);
		//默认选中矩形
		rectangleButton.setSelected(true);
		
		//这个面板用来在UI上容纳3个button,添加需要按显示顺序添加
		JPanel jPanel = new JPanel(new FlowLayout());
		jPanel.add(lineButton);
		jPanel.add(ovalButton);
		jPanel.add(rectangleButton);
		this.addMouseListener(this);
		this.setLayout(new BorderLayout());
		this.add(jPanel,BorderLayout.NORTH);
		
		if (mode == 1) {
			this.setBounds(100, 200, 500, 500);
		}
		else if (mode == 2) {
			this.setBounds(800, 200, 500, 500);
		}
		Out.aprintln("SimpleDraw() OK");
	}

	
	
	@Override
	public void paint(Graphics g) {
		super.paint(g);
//		paintComponents(g);
		for (Shape shape:shapes) {
			shape.draw(g);
		}
		Out.aprintln("shapes1 size:"+shapes.size());
	}

	public void paint1(Graphics g) {
//		paintComponents(g);
		if(shapes.size() > 0)shapes.get(shapes.size()-1).draw(g);
		Out.aprintln("shapes2 size:"+shapes.size());
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		Out.aprintln("mousePressed");
		x1 = e.getX();
		y1 = e.getY();
	}


	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		Out.aprintln("mouseReleased");
		x2 = e.getX();
		y2 = e.getY();
		Shape shape = null;
		if (shapeType.equals("Rectangle")) {
			//必须两个坐标连线不平行于XY轴
			if(x1 != x2 && y1 != y2){
				shape = new Rectangle(x1, y1, x2, y2);
			}
		}
		else if(shapeType.equals("Line")){
			//两个点只要x或y其一不等，即两个不同的点
			if (x1 != x2 || y1 != y2) {
				shape = new Line(x1, y1, x2, y2);
			}
			
		}
		else if(shapeType.equals("Oval")){
			if(x1 != x2 && y1 != y2){
				shape = new Oval(x1, y1, x2, y2);
			}
		}
		if (shape != null) {
			if(shapes.size()>=20)shapes.clear();
			this.shapes.add(shape);
			if (paintMode == 1) {
				this.repaint();//此处会调用Paint方法，重绘整个JFrame,会闪烁
			}
			else if (paintMode == 2) {
				this.paint1(getGraphics());//只绘画最新添加的shape
			}
		}
	}


	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		shapeType = e.getActionCommand().toString();
	}


	private static void constructGUI() {
		JFrame.setDefaultLookAndFeelDecorated(false);//使用windows风格，切换窗口时另一个窗口的绘画不会消失
		SimpleDraw frame = new SimpleDraw(1);
//		frame.pack();
		frame.setVisible(true);
		
		SimpleDraw frame2 = new SimpleDraw(2);
//		frame2.pack();
		frame2.setVisible(true);
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				constructGUI();// 使用static方法或创建对象
			}
		});
	}

}
